package com.mllweb.blog.service.impl;

import com.mllweb.blog.core.cache.RedisClient;
import com.mllweb.blog.core.cache.RedisKey;
import com.mllweb.blog.data.dao.BlogLoginModeDao;
import com.mllweb.blog.data.orm.BlogLoginMode;
import com.mllweb.blog.service.RedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @Prject: blog-server
 * @Package: com.mllweb.blog.service.impl
 * @author: feejson
 * @date: 2017/9/1 12:29
 * @version: V1.0
 */
@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private BlogLoginModeDao blogLoginModeDao;

    @Override
    public int getLoginMode(String email) {

        if (!redisClient.exist(RedisKey.LOGIN_MODE.getValue())) {
            List<BlogLoginMode> modes = blogLoginModeDao.select();
            redisClient.set(RedisKey.LOGIN_MODE.getValue(), modes);
        }
        List<BlogLoginMode> modes = redisClient.get(RedisKey.LOGIN_MODE.getValue(), BlogLoginMode.class);
        for (int i = 0, size = modes.size(); i < size; i++) {
            BlogLoginMode mode = modes.get(i);
            if (StringUtils.equals(email, mode.getMode())) {
                return mode.getId();
            }
        }
        return 1;
    }

    @Override
    public String getEmailWeb(String loginName) {
        return redisClient.get(loginName.substring(loginName.indexOf('@')+1));
    }
}
